package com.zline.foodraise.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.zline.foodraise.R;
import com.zline.foodraise.constant.Constant;
import com.zline.foodraise.entity.UserBean;
import com.zline.foodraise.http.HttpErro;
import com.zline.foodraise.http.HttpOperator;
import com.zline.foodraise.http.HttpUtil;
import com.zline.foodraise.util.EmojiFilter;
import com.zline.foodraise.util.LUtil;
import com.zline.foodraise.util.NetworkUtil;
import com.zline.foodraise.util.StringUtils;
import com.zline.foodraise.util.TUtil;
import com.zline.foodraise.view.materialedittext.MaterialEditText;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;
import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by heshantao on 15/6/30.
 * 签名
 */
@EActivity(R.layout.activity_signature_set)
public class SignatureSetActivity extends ZlineActivity {
    private static final String LOG_TAG = SignatureSetActivity.class.getSimpleName();
    @ViewById(R.id.app_common_header_left_icon_iv)
    ImageView back_iv;
    @ViewById(R.id.app_common_header_title_tv)
    TextView title_tv;
    @StringRes(R.string.personal_signature)
    String titleRes;
    @ViewById(R.id.app_common_header_right_tv)
    TextView save_tv;
    @ViewById(R.id.signature_set_mtv)
    MaterialEditText signature_met;
    @Extra
    String signature;
    @Bean
    HttpOperator httpOperator;
    @Bean
    HttpUtil httpUtil;
    int resultCode = 0;
    @Bean
    HttpErro httpErro;
    @Bean
    NetworkUtil networkUtil;

    @AfterViews
    void doSomeAfterViews() {
        title_tv.setText(titleRes);
        save_tv.setVisibility(View.VISIBLE);
        if (networkUtil.isNetworkAvailable()) {
            signature_met.setText(signature);
        } else {
            TUtil.showShort(this, R.string.fail_load_data);
        }
    }

    //请求修改用户签名
    void requestModifySignature() {
        signature = signature_met.getText().toString().trim();
        signature = EmojiFilter.filterEmoji(signature);
        if (StringUtils.isBlank(signature)) {
            TUtil.showShort(this, R.string.null_signature);
        } else if (signature.length() > 30) {
            TUtil.showShort(this, R.string.long_signature);
        } else {
            if (networkUtil.isNetworkAvailable()) {
                showLoadingDialog();
                httpOperator.requstModifySignature(httpUtil, signature, requestModifySignatureHandler);
            } else {
                TUtil.showShort(this, R.string.no_network);
            }

        }
    }

    //修改签名回调
    AsyncHttpResponseHandler requestModifySignatureHandler = new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);
            LUtil.i(LOG_TAG, "____successs_____requestModifySignatureHandler_____" + statusCode + response.toString());
            if (statusCode == 200) {
                try {
                    resultCode = response.getInt(Constant.RESULT_CODE);
                    if (resultCode == 200) {//调用成功
                        setResult();
                    } else {
                        httpErro.showCommonErroResult(resultCode);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            dissLoadingDialog();
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            super.onFailure(statusCode, headers, responseString, throwable);
            dissLoadingDialog();
        }
    };

    void setResult() {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString(Constant.VALUE, signature);
        intent.putExtras(bundle);
        setResult(Constant.SIGNATURE_RESULT_CODE, intent);
        finish();
    }

    @Click({R.id.app_common_header_left_icon_iv, R.id.app_common_header_right_tv})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.app_common_header_left_icon_iv:
                goback();
                break;
            case R.id.app_common_header_right_tv:
                requestModifySignature();
                break;
            default:
                break;
        }

    }

}
